<?php
class co extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览社区圈子', '1-2');
		$da = $this -> pub -> get_setting('社区圈子', 'CO');
		$this -> load -> view('adminx/co', $da);
	}

	function get() {
		$this -> pub -> logp('获取社区圈子信息', '1-2');
		if ($this -> input -> post('key')) {
			$this -> db -> select('count(*)');
			$this -> db -> like('name', $this -> input -> post('key'));
			if ($this -> input -> post('type') > 0) {
				$this -> db -> where('type', $this -> input -> post('type'));
			}
			if ($this -> input -> post('state') > 0) {
				$this -> db -> where('state', $this -> input -> post('state'));
			}
			$q = $this -> db -> get_where('co_main');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
			$this -> db -> like('name', $this -> input -> post('key'));
		} else {
			if ($this -> input -> post('type') > 0) {
				$this -> db -> where('type', $this -> input -> post('type'));
			}
			if ($this -> input -> post('state') > 0) {
				$this -> db -> where('state', $this -> input -> post('state'));
			}
			$this -> db -> select('count(*)');
			$q = $this -> db -> get_where('co_main');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
		}
		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> order_by('id', 'desc');
		if ($this -> input -> post('type') > 0) {
			$this -> db -> where('type', $this -> input -> post('type'));
		}
		if ($this -> input -> post('state') > 0) {
			$this -> db -> where('state', $this -> input -> post('state'));
		}
		$q = $this -> db -> get_where('co_main');
		$da['content'] = '';
		$type[1] = '<span class="label label-sm label-info">系统群</span>';
		$type[2] = '<span class="label label-sm label-success">个人群</span>';
		$type[3] = '<span class="label label-sm label-danger">自定义群</span>';

		$state[1] = '<span class="label label-sm label-success">正常运行</span>';
		$state[2] = '<span class="label label-sm label-danger">全局闭嘴</span>';
		$state[3] = '<span class="label label-sm label-danger">停止运行</span>';

		$gz[1] = '<span class="label label-sm label-info">全开放</span>';
		$gz[2] = '<span class="label label-sm label-success">大东家开放</span>';
		$gz[3] = '<span class="label label-sm label-danger">审核后开放</span>';
		$gz[4] = '<span class="label label-sm label-danger">购买某款商品</span>';
		foreach ($q->result_array() as $v) {
			$this -> db -> select('headimgurl');
			$qq = $this -> db -> get_where('member', array('mid' => $v['mid']));
			$m = $qq -> row_array(0);
			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $v['id'] . '</td>';
			$da['content'] .= '<td><a target="_blank" href="/iadmin/member/info/' . $v['mid'] . '"><img width="48" src="' . ($m['headimgurl'] ? $m['headimgurl'] : '/mex/lance/img/tx.jpg') . '" /></a></td>';
			$da['content'] .= '<td><img width="48" src="' . $this -> pub -> oss_dz($v['pic']) . '"></td>';
			$da['content'] .= '<td>' . $v['name'] . '</td>';
			$da['content'] .= '<td>' . $v['yonyou_name'] . '</td>';
			$da['content'] .= '<td>' . $type[$v['type']] . ' ' . $state[$v['state']] . ' ' . $gz[$v['gz']] . '</td>';
			$da['content'] .= '<td>' . ($v['mktime'] ? date('Y-m-d', $v['mktime']) : '') . '</td>';
			$da['content'] .= '<td><button onclick="show_up(\'' . $v['id'] . '\')" type="button" class="btn default"><i class="fa fa-edit"></i>修改</button> <button onclick="alert_ts(\'社群原链接是：' . site_url('wrq/main/' . $v['gid']) . '\')" type="button" class="btn btn-info"><i class="fa fa-link"></i>链接</button>';
			$da['content'] .= '</td></tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

	function add() {
		$this -> load -> helper('string');
		$this -> pub -> logp('创建社区圈子', '1-2');
		$da['name'] = $this -> input -> post('name');
		$da['mid'] = $this -> input -> post('mid');
		$da['jj'] = $this -> input -> post('jj');
		$da['state'] = $this -> input -> post('state');
		$da['type'] = $this -> input -> post('type');
		$da['gz'] = $this -> input -> post('gz');
		$da['pic'] = $this -> input -> post('pic');

		if ($this -> config -> item('is_app')) {
			$da['price'] = $this -> input -> post('price');
		}

		$da['sp_id'] = $this -> input -> post('sp_id');
		$da['gid'] = random_string('alnum', 32);
		$da['mktime'] = mktime();
		$this -> db -> insert('co_main', $da);

		//$id = $this -> db -> insert_id();
		//$this -> csh($id);
		echo json_encode(array('type' => 1));
	}

	function up() {
		$this -> pub -> logp('社区圈子修改 - ' . $this -> input -> post('idx'), '1-2');
		$da['name'] = $this -> input -> post('name');
		$da['mid'] = $this -> input -> post('mid');
		$da['sp_id'] = $this -> input -> post('sp_id');
		$da['jj'] = $this -> input -> post('jj');
		if ($this -> input -> post('pic')) {
			$da['pic'] = $this -> input -> post('pic');
		}

		if ($this -> config -> item('is_app')) {
			$da['price'] = $this -> input -> post('price');
		}

		$da['state'] = $this -> input -> post('state');
		$da['type'] = $this -> input -> post('type');
		$da['gz'] = $this -> input -> post('gz');
		$this -> db -> update('co_main', $da, array('id' => $this -> input -> post('idx')));

		//$this -> up_qun($this -> input -> post('idx'));

		echo json_encode(array('type' => 1));
	}

	function get_one() {
		$this -> pub -> logp('获取社区圈子信息 - ' . $this -> input -> post('idx'), '1-2');
		$q = $this -> db -> get_where('co_main', array('id' => $this -> input -> post('idx')));
		if ($q -> num_rows() > 0) {
			$da = $q -> row_array(0);
			$da['typex'] = 1;
			echo json_encode($da);
		} else {
			echo json_encode(array('typex' => 2));
		}
	}

	function up_qun($id) {
		$this -> load -> model('yonyou');
		$q = $this -> db -> get_where('co_main', array('id' => $id));
		$sq = $q -> row_array(0);

		$back = $this -> yonyou -> up_qun($sq);
	}

	//初始化 群
	function csh($id) {
		$this -> load -> model('yonyou');
		$q = $this -> db -> get_where('co_main', array('id' => $id));
		$sq = $q -> row_array(0);

		$back = $this -> yonyou -> add_qun($sq);
	}

}
?>